package com.liao.furn.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liao.furn.pojo.Furn;
import com.liao.furn.pojo.Msg;
import com.liao.furn.service.FurnService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */
@Controller
public class FurnController {

    @Resource
    private FurnService furnService;

    @PostMapping(value = "/save")
    @ResponseBody
    public Msg save(@Validated  @RequestBody Furn furn, Errors errors){
        List<FieldError> fieldErrors = errors.getFieldErrors();
        HashMap<String, Object> map = new HashMap<>();
        for (FieldError fieldError : fieldErrors) {
            map.put(fieldError.getField(),fieldError.getDefaultMessage());
        }

        if(map.isEmpty()){
            furnService.save(furn);
            Msg success = Msg.success();
            return success;
        }else {
            return Msg.fail().add("errorMsg",map);
        }

    }

    @ResponseBody
    @RequestMapping("/furns")
    public Msg listFurns() {
        List<Furn> furnList = furnService.findAll();
        Msg msg = Msg.success();
        msg.add("furnList",furnList);
        return msg;
    }

    @PutMapping("/update")
    @ResponseBody
    public Msg update(@RequestBody Furn furn) {
         furnService.update(furn);
         return Msg.success();
    }

    @GetMapping ("/select/{id}")
    @ResponseBody
    public Msg selectById(@PathVariable("id") Integer id) {
        Furn furn = furnService.selectByPrimaryKey(id);
        return Msg.success().add("furn",furn);
    }

    @DeleteMapping(value = "/delete/{id}")
    @ResponseBody
    public Msg delete(@PathVariable("id") Integer id){
        furnService.deleteByKey(id);
        Msg success = Msg.success();
        return success;
    }

    @ResponseBody
    @RequestMapping("/furnsByPage")
    public Msg listFurnsByPage(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Furn> furnList = furnService.findAll();
        PageInfo pageInfo = new PageInfo(furnList, pageSize);
        return Msg.success().add("pageInfo",pageInfo);
    }


    @ResponseBody
    @RequestMapping("/furnsByConditionPage")
    public Msg listFurnsByConditionPage(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize
                                        ,@RequestParam(defaultValue = "") String search){
        PageHelper.startPage(pageNum,pageSize);
        List<Furn> furnList = furnService.findByCondition(search);
        PageInfo pageInfo = new PageInfo(furnList, pageSize);
        return Msg.success().add("pageInfo",pageInfo);
    }
}
